package com.wangsd.web.coupon.controller;


import com.wangsd.common.basicbean.Result;
import com.wangsd.web.MyController;
import com.wangsd.web.coupon.entity.*;
import com.wangsd.web.coupon.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

import static com.wangsd.common.enums.ResultCode.NO_LOGIN;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author generator
 * @since 2019-09-10
 */
@Api(value = "wxBigWheel", tags = "大转盘")
@RestController
@RequestMapping("/coupon/wxBigWheel")
@Slf4j
public class WxBigWheelController extends MyController {

    @Autowired
    BigWheelService bigWheelService;

    @Autowired
    BigWheelDetailService bigWheelDetailService;

    @Autowired
    CompanyService companyService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    /**
     * 查询幸运大转盘列表
     */
    @ApiOperation(value = "查询幸运大转盘列表", notes = "查询幸运大转盘列表", httpMethod = "POST")
    @PostMapping("/selectBigWheelList")
    public Result selectBigWheelList() {
        Company company = companyService.selectByMiniAppid(getMiniAppid());
        try {
            BigWheel bigWheel = bigWheelService.selectBigWheelByCompanyId(company.getId());
            List<BigWheelDetail> bigWheelDetails = bigWheelDetailService.selectBigWheelList(company.getId());
            bigWheel.setBigWheelDetailList(bigWheelDetails);
            return Result.success(bigWheel);
        } catch (Exception e) {
            log.error("selectBigWheelList", e);
        }
        return Result.fail("查询幸运大转盘列表失败");
    }

    /**
     * 开始启动幸运大转盘
     */
    @ApiOperation(value = "开始启动幸运大转盘", notes = "开始启动幸运大转盘", httpMethod = "GET")
    @GetMapping("/runBigWheel")
    public Result runBigWheel() {
        Member miniUserInfo = getMiniUserInfo();
        if (miniUserInfo == null) {
            return new Result(NO_LOGIN);
        }
        String openId = miniUserInfo.getMiniOpenid();
        Company company = companyService.selectByMiniAppid(getMiniAppid());
        BigWheel wheel = bigWheelService.selectBigWheelByCompanyId(company.getId());
        /*判断是否在有效时间*/
        Date now = new Date();
        Date beginTime = wheel.getBeginTime();
        Date endTime = wheel.getEndTime();
        if (now.compareTo(beginTime) < 0 || now.compareTo(endTime) > 0) {
            return Result.fail("不在大转盘活动时间！");
        }

        /*
         * 判断redis中已经存在次数限制，如果不存在，就将 最新的次数 - 1 添加到redis中
         * 如果有次数限制，则直接减一后添加到redis中。
         * 并且当天的数据在零点使用定时任务进行清除
         * */
        String s = stringRedisTemplate.opsForValue().get("bigWheel" + openId);
        Integer times = wheel.getTimes();
        if (times == null || times == 0) {
            return Result.fail("商圈没有配置抽奖次数");
        }
        if (s == null) {
            times = times - 1;
            stringRedisTemplate.opsForValue().set("bigWheel" + openId, times.toString());
        } else {
            Integer integer = Integer.valueOf(s);
            if (integer == 0) {
                return Result.fail("你的抽奖次数已使用完！");
            }
            times = integer - 1;
            stringRedisTemplate.opsForValue().set("bigWheel" + openId, times.toString());
        }
        BigWheelDetail bigWheel = bigWheelDetailService.runBigWheel(company.getId(), openId);
        bigWheel.setTimes(times);
        return Result.success(bigWheel);
    }

}